package com.gzk.hrm.config;

import com.gzk.hrm.handler.DefaultAccessDeniedHandler;
import com.gzk.hrm.handler.MyAuthenticationFailureHandler;
import com.gzk.hrm.handler.MyAuthenticationSuccessHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.util.List;

@Configuration
@EnableWebSecurity
@Slf4j
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    //密码编码器：加密
    @Bean
    public PasswordEncoder passwordEncoder(){
        return new BCryptPasswordEncoder();
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode("123");
        System.out.println(encode);

    }

    //授权规则配置
    @Override
    protected void configure(HttpSecurity http) throws Exception {

       /* //查询出所有的权限
        List<Permission> permissions = permissionMapper.selectAll();

        //遍历
        permissions.forEach(permission -> {
            try {
                //http.authorizeRequests().antMatchers(permission.getResource()).hasAuthority(permission.getExpression());
                //log.info("加载资源 {}，需要权限{}",permission.getResource(),permission.getExpression());
            } catch (Exception e) {
                e.printStackTrace();
            }
        });*/

        /*http.exceptionHandling()
                .accessDeniedHandler(new DefaultAccessDeniedHandler())
                .authenticationEntryPoint(new DefaultAccessDeniedHandler());*/


        http.authorizeRequests()                                //授权配置
                .antMatchers("/login","/login/user","/oauth-resource","/login.html").permitAll()  //登录路径放行
                .anyRequest().authenticated()                   //其他路径都要认证之后才能访问
                .and().formLogin()                              //允许表单登录
                //.loginPage("/login.html")                       //登录页面跳转地址
                //.loginProcessingUrl("/login")                   //登录处理地址(必须)
                .successForwardUrl("/login/loginSuccess")             // 设置登陆成功页
                .failureHandler(new MyAuthenticationFailureHandler())//配置失败处理器
                //.successHandler(new MyAuthenticationSuccessHandler())//配置成功处理器
                .and().logout().permitAll()                    //登出路径放行 /logout
                .and().csrf().disable();                        //关闭跨域伪造检查
    }

    //配置认证管理器，授权模式为“poassword”时会用到
    @Bean
    public AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }
}